package smartcounters.androidclient.WebServices;

import java.io.IOException;
import java.util.LinkedList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Message;

import smartcounters.androidclient.model.eOperationsEnum;
import smartcounters.androidclient.model.UserCredentials;
import smartcounters.androidclient.model.AuthenticatedUserCredentials;

public class AuthenticationWebServiceClient implements OnResultReceiver{
	private LinkedList<OnResultReceiver> m_ResultReceivers;
	
	private static final String NAMESPACE = "http://smartcounters.org";
	private static final String GET_SOAP_ACTION = "http://smartcounters.org/Authenticate";
	private static final String GET_METHOD_NAME = "Authenticate";
	
	private String m_URL;
	
	public AuthenticationWebServiceClient(String serverIP){
		m_ResultReceivers = new LinkedList<OnResultReceiver>();
		
		m_URL = "http://" + serverIP + ":50171/WebServices/AuthenticationWebService.asmx?WSDL";
	}
	
	public void Authenticate(UserCredentials i_UserCredentials) throws IOException, XmlPullParserException{
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_Username");
		usernameObject.addProperty("i_Username", i_UserCredentials.getUsername());
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_Password");
		passwordObject.addProperty("i_Password", i_UserCredentials.getPassword());
		
		request.addSoapObject(usernameObject);
		usernameObject.addSoapObject(passwordObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.Authenticate);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void RegisterResultReceiver(OnResultReceiver i_ResultReceiver ) {
		m_ResultReceivers.add(i_ResultReceiver);
	}
	
	protected void OnResultReceived(Message i_Msg){
		if (!(i_Msg.obj instanceof ConnectionError)){
			SoapObject resultsRequestSOAP = (SoapObject) i_Msg.obj;
			if (resultsRequestSOAP.getPropertyCount() == 0) {
				i_Msg.obj = null;
			}
			else {
				SoapObject soapObject = (SoapObject)resultsRequestSOAP.getProperty(0);
				AuthenticatedUserCredentials auth = new AuthenticatedUserCredentials(Integer.parseInt(soapObject.getProperty("ID").toString()), soapObject.getProperty("Password").toString());
				
				i_Msg.obj = auth;
			}
		}
		
		for (OnResultReceiver resultReceiver : m_ResultReceivers) {
			resultReceiver.ReceiveResult(i_Msg);
		}
	}

	public void ReceiveResult(Message i_Msg) {
		OnResultReceived(i_Msg);
	}
}